<!--
* @description 诛仙登录
* @fileName login.vue
* @author huangyu
* @date 2023/02/14 14:09:42
!-->
<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
            <el-form-item label="摆摊号" prop="gameCode">
                <el-input v-model="ruleForm.gameCode" placeholder="请输入摆摊号"></el-input>
            </el-form-item>
            <!-- <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" show-password placeholder="请输入密码"></el-input>
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" @click="login('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { get } from '@/common/app.js'
import * as types from "@/store/types";
// import md5 from 'js-md5';
export default {
    components: {},
    props:{
        routerLink: String,
    },
    data() {
        return {
            token: "",
            ruleForm: {
                gameCode: "",
            },

            rules: {
                gameCode: [{ required: true, message: '请输入账号', trigger: 'blur' },],
                // password: [{ required: true, message: '请输入密码', trigger: 'blur' },],
            },
        }
    },
    mounted() {
    },
    methods: {
        login(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loginData()
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        loginData(){
            // this.ruleForm.password = md5(this.ruleForm.password)
            let params = {
                type: 'getLogin',
                api: 'api-login',
                data: this.ruleForm,
            }
            // console.log(params)
            get('game', params).then(res => {
                if (res) {
                    // console.log(res)
                    if(res.type=='error'){
                        this.$message.error(res.msg)
                        return
                    }

                    this.token = res._id
                    // 存储token并跳转
                    if(this.token){
                        let logindata = {
                            token: this.token,
                            user: {
                                gameCode: res.gameCode,
                                gameName: res.gameName,
                                qq: res.qq,
                                gameId: res.gameId,
                                userId: res._id,
                            },
                            power: res.powerInfo,
                        }
                        this.$message.success("登录成功！")
                        this.$store.commit(types.LOGIN, logindata);
                            let redirect = decodeURIComponent(
                            this.$route.query.redirect || this.routerLink
                        );
                        this.$router.push({
                            path: redirect
                        });
                        this.$emit('onLogin',true)
                        this.$router.go(0)
                    }
                }
            })
        },
        
    },
}
</script>

<style scoped>

</style>